package com.stephen.data.tree;

/**
 * @author: stephen
 * @create: 2020-02-01 17:59
 * @description: 二叉树
 */
public class BinaryTreeDemo {

    public static void main(String[] args) {

        Node root = new Node(1, "a");
        Node b = new Node(2, "b");
        Node c = new Node(3, "c");
        Node e = new Node(4, "e");
        Node f = new Node(5, "f");
        Node g = new Node(6, "g");
        Node h = new Node(7, "h");

        root.setLeft(b);
        root.setRight(c);
        b.setLeft(e);
        b.setRight(f);
        c.setLeft(g);
        c.setRight(h);

        BinaryTree tree = new BinaryTree(root);
        System.out.println("前序遍历");
        tree.preOrder(); // 1,2,4,5,3,6,7
        // System.out.println("中序遍历");
        // tree.inFixOrder();
        // System.out.println("后序遍历");
        // tree.postOrder();
        // System.out.println("前序查找");
        // System.out.println(tree.preOrderSearch(6));
        // System.out.println("中序查找");
        // System.out.println(tree.infixOrderSearch(6));
        // System.out.println("后序查找");
        // System.out.println(tree.postOrderSearch(6));
        tree.delNode(3);
        System.out.println("删除后,前序遍历");
        tree.preOrder(); // 1,2,4,5
    }

}

class BinaryTree {
    private Node root; // 根节点

    public BinaryTree(Node root) {
        this.root = root;
    }

    /**
     * 删除节点
     *
     * @param no
     */
    public void delNode(int no) {
        if (root != null) {
            if (root.getId().equals(no)) {
                root = null;
            } else {
                this.root.delNode(no);
            }
        } else {
            System.out.println("空树,无法删除~~~");
        }
    }

    /**
     * 前序遍历查找
     *
     * @param no 要查找的id
     * @return
     */
    public Node preOrderSearch(int no) {
        return this.root.preOrderSearch(no);
    }

    /**
     * 中序遍历查找
     *
     * @param no 要查找的编号
     * @return
     */
    public Node infixOrderSearch(int no) {
        return this.root.infixOrderSearch(no);
    }

    /**
     * 后序遍历查找
     *
     * @param no 要查找的编号
     * @return
     */
    public Node postOrderSearch(int no) {
        return this.root.postOrderSearch(no);
    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        if (root != null) {
            root.preOrder();
        } else {
            System.out.println("树为空~~~");
        }
    }

    /**
     * 中序遍历
     */
    public void inFixOrder() {
        if (root != null) {
            root.inFixOrder();
        } else {
            System.out.println("树为空~~~");
        }
    }

    /**
     * 后序遍历
     */
    public void postOrder() {
        if (root != null) {
            root.postOrder();
        } else {
            System.out.println("树为空~~~");
        }
    }

}

class Node {
    private Integer id;
    private String name;
    private Node left; // 左节点
    private Node right; // 右节点

    public Node(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Node getLeft() {
        return left;
    }

    public Node getRight() {
        return right;
    }

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    /**
     * 删除节点
     * 暂时规定:
     * 1.叶子结点,直接删除
     * 2.非叶子结点,直接删除所在子树
     *
     * @param no
     */
    public void delNode(int no) {
        // 先看左节点是否是删除的节点
        if (this.left != null && this.left.id == no) {
            this.left = null;
            return;
        }
        // 再看右节点是否是删除的节点
        if (this.right != null && this.right.id == no) {
            this.right = null;
            return;
        }
        // 向左递归试着删除
        if (this.left != null) {
            this.left.delNode(no);
        }

        // 向右递归试着删除
        if (this.right != null) {
            this.right.delNode(no);
        }
    }

    /**
     * 后序查找
     *
     * @param no 要查找的序号
     * @return
     */
    public Node postOrderSearch(int no) {
        Node resNode = null;

        // 先查找左边
        if (this.left != null) {
            resNode = this.left.postOrderSearch(no);
        }

        if (resNode != null) {
            return resNode;
        }

        // 再查找右边
        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }

        if (resNode != null) { // 找到就立马返回
            return resNode;
        }

        System.out.println("后序查找" + this);
        // 再查找自己
        if (this.id.equals(no)) {
            return this;
        }
        return null;
    }

    /**
     * 中序查找
     *
     * @param no 要查找的序号
     * @return
     */
    public Node infixOrderSearch(int no) {

        Node resNode = null;

        // 先查找左边
        if (this.left != null) {
            resNode = this.left.infixOrderSearch(no);
        }

        if (resNode != null) {
            return resNode;
        }

        System.out.println("中序查找" + this);
        // 再查找自己
        if (this.id.equals(no)) {
            return this;
        }
        // 再查找右边
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    /**
     * 前序查找,递归的思想
     *
     * @param no
     * @return
     */
    public Node preOrderSearch(int no) {
        System.out.println("前序查找" + this);
        // 1. 先判断当前节点是否相等
        if (this.getId().equals(no)) {
            return this;
        }

        Node resNode = null;

        // 2. 再判断当前节点的左节点
        if (this.getLeft() != null) {
            resNode = this.getLeft().preOrderSearch(no);
        }

        if (resNode != null) {
            return resNode;
        }

        // 3. 再判断当前节点的右节点
        if (this.getRight() != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    /**
     * 中序遍历
     */
    public void inFixOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.inFixOrder();
        }
        if (this.right != null) {
            this.right.inFixOrder();
        }
    }

    /**
     * 后序遍历
     */
    public void postOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
    }

}